<template>
	<view class="mainbgc" @click="hidefalse">
		<!-- 整单备注 -->
		<u-popup :show="xsshow" :round="10" mode="center">
			<view class="zhengds ">
				<view class="strongtext" style="text-align: center;">请输入整单备注</view>
				<input type="text" class="shurudh margin_top" placeholder="请输入整单备注" v-model="note_text">
				<view class="flexcenter margin_top">
					<view class="btns btn1" @click="xsclose">取消</view>
					<view class="btns btn2" @click="surenote">确定</view>
				</view>
			</view>
		</u-popup>
		
		<!-- 优惠 -->
		<u-popup :show="yhshow" :round="10" mode="center">
			<view class="zhengds ">
				<view class="strongtext" style="text-align: center;">请输入优惠金额</view>
				<input type="text" class="shurudh margin_top" placeholder="请输入优惠金额" v-model="dataAll.preferential_money">
				<view class="flexcenter margin_top">
					<view class="btns btn1" @click="yhclose">取消</view>
					<view class="btns btn2" @click="suremoney">确定</view>
				</view>
			</view>
		</u-popup>
		<!-- 默认售价 -->
		<u-popup :show="mrshow" :round="10" mode="center" @close="mrclose">
			<view class="zhengds">
				<view class="strongtext mainpadding2" style="text-align: center;">请选择默认售价</view>
				<view class="fangan">
					<!-- 档案价格 -->
					<view class="lansetextwbk margin_top">档案价格</view>
					<view class="margin_top">
						<view class="flexcenter margin_top xiabiakuang " @click="switchsj('wholesale_price')">
							<view class="duihao" v-show="sj_current=='wholesale_price'"></view>
							<view class=" nostrongtext juzhong">批发价格</view>
						</view>
						<view class="flexcenter margin_top xiabiakuang " @click="switchsj('market_sell_price')">
							<view class="duihao" v-show="sj_current=='market_sell_price'"></view>
							<view class=" nostrongtext juzhong">零售价格</view>
						</view>
						<view class="flexcenter margin_top xiabiakuang " @click="switchsj('last_price')">
							<view class="duihao" v-show="sj_current=='last_price'"></view>
							<view class=" nostrongtext juzhong">上次售价</view>
						</view>
					</view>
				</view>
			</view>
		</u-popup>
		<header-top :messageInfo="{typeFlag:true,message:'销售单',liner:false}">
			<view class="rightmsg" slot="rightmsg" v-if="getmsg.type!=3">
				<view class="rightmsg_content flexleft">
					<view class="nostrongtext margin_right" @click="tuisong" v-if="getmsg.type==1">推送</view>
					<view class="diandiandian" @click.stop="dian">
						<view class="selectcontent" v-show="selectshow">
							<view class="topbox">
								<view class="flexleft itemselect" @click.stop="lishidanju">
									<view class="leftimage margin_right">
										<image src="../../../static/image/system/lishidanj.png" mode=""></image>
									</view>
									<view>历史单据</view>
								</view>
							</view>
							<view class="topbox" @click.stop="morenshouj">
								<view class="flexleft itemselect">
									<view class="leftimage margin_right">
										<image src="../../../static/image/system/morensj.png" mode=""></image>
									</view>
									<view>默认售价</view>
								</view>
							</view>
							<view class="topbox">
								<view class="flexleft itemselect" @click.stop="youhui">
									<view class="leftimage margin_right">
										<image src="../../../static/image/system/youhui.png" mode=""></image>
									</view>
									<view>优惠</view>
								</view>
							</view>
							<view class="topbox">
								<view class="flexleft itemselect" @click.stop="xiaosbz">
									<view class="leftimage margin_right">
										<image src="../../../static/image/system/beizhu.png" mode=""></image>
									</view>
									<view>整单备注</view>
								</view>
							</view>
							<view class="topbox">
								<view class="flexleft itemselect" @click.stop="qingkong" v-if="getmsg.type==2">
									<view class="leftimage margin_right">
										<image src="../../../static/image/system/shanchu.png" mode=""></image>
									</view>
									<view>取消下单</view>
								</view>
							</view>
							<view class="sanjiao"></view>
						</view>
					</view>
				</view>
			</view>
		</header-top>
		<!-- 顶部按钮 及单位内容-->
		<view class="margin_top mainpadding ffffff">
			<view class="flexleft">
				<view class="lansebtn allbtn margin_right">
					{{dataAll.customer_name}}
				</view>
				<view :class="cangku.name?'lansebtn allbtn':'allbtn huisebtn'" @click="cangkuShow = true">
					{{cangku.name || '选择仓库'}}
				</view>
			</view>
			<view class="flexbetween margin_top">
				<view class="strongtext">结算单位：{{dataAll.customer_name}}</view>
				<view class="nostrongtext">{{dataAll.settlementtype_name}}</view>
			</view>
			<view class="flexleft margin_top">
				<view class="lansetext" v-show="dataAll.arrear_account" @click="yushoukuan">
					欠款:{{dataAll.arrear_account}}</view>
				<view class="lansetext" v-show="dataAll.receive_account" @click="yushoukuan">
					预收款:{{dataAll.receive_account}}</view>
				<view class="lansetext" v-show="dataAll.unreturned_account">未还货:{{dataAll.unreturned_account}}</view>
			</view>
		</view>
		<!-- 商品信息 -->
		<view class=" margin_top shangpinxix ffffff" v-if="dataAll.goods_info.length!=0">
			<view class="flexbetween">
				<view class="titletext flex2">商品名称</view>
				<view class="titletext flex2s">规格</view>
				<view class="titletext flex1s">数量</view>
				<view class="titletext flex1s">单价</view>
			</view>
			<view class="shangpinbox" v-for="item in dataAll.goods_info" :key="item.id" @click="xgdjmx(item)">
				<view class="flexbetween margin_top">
					<view class="nostrongtext flex2">{{item.goods_name}}
					</view>
					<view class="nostrongtext flex2s">{{item.unit_name}}</view>
					<view class="strongtext flex1s">*{{item.purchase_type==2?'-':''}}{{item.number}}</view>
					<view class="strongtext flex1s" v-if="item.purchase_type!=3">{{item.unit_price}}</view>
					<view class="hongse strongtext  flex1s" v-if="item.purchase_type==3">赠品</view>
				</view>
				<view class="flexbetween margin_top">
					<view class="huisetext">备注：{{item.remark}}</view>
					<view class="lansetextwbk" v-if="item.purchase_type!=3">￥{{item.purchase_type==2?'-':''}}{{item.all_money}}</view>
				</view>
			</view>
			<view class="flexbetween paddingsx">
				<view class="littlesmall flex2">合计：</view>
				<view class="littlesmall flex2s">{{dataAll.goods_kind}}（含赠品）</view>
				<view class="littlesmall flex1s">{{Number(this.dataAll.number) - Number(this.dataAll.return_number)}}</view>
				<view class="littlesmall flex1s" style="color: #0BCB41;">{{Number(this.dataAll.all_money) - Number(this.dataAll.return_money)}}元</view>
			</view>
		</view>
		<!-- 合计钱数 -->
		<view style="padding-bottom: 200rpx;" v-if="dataAll.goods_info.length!=0">
			<view class="heji mainpadding2">
				<view class="flexleft">
					<view class="smalltext xiaoshou">销售:
						<text class="lvse">{{dataAll.all_money}}元</text>
					</view>
					<view class="smalltext xiaoshou">优惠:
						<text class="lvse">{{dataAll.preferential_money}}元</text>
					</view>
				</view>
				<view class="flexleft">
					<view class="smalltext xiaoshou">合计:
						<text class="lvse">{{Number(this.dataAll.all_money) - Number(this.dataAll.return_money)}}元</text>
					</view>
					<view class="smalltext xiaoshou" v-for="item in dataAll.pay_data" :key="item.accounts_id">{{item.account_name}}:
						<text class="lvse">{{item.money}}元</text>
					</view>
					<view class="smalltext">定货款:
						<text class="lvse">{{dataAll.all_money}}元</text>
					</view>
				</view>
			</view>
		</view>

		<view class="ffffff dibu flexbetween">
			<view class="shuruk flexbetween">
				<input type="text" class="inputs" disabled="" placeholder="输入商品首拼/编号" disabled @click="tianjia(1)">
				<view class="tianjia" @click="tianjia(2)">添加</view>
			</view>
			<view class="flexleft">
				<view class="scan2" ></view>
				<view class="yuyin"></view>
			</view>
		</view>
		<!-- 仓库档案 -->
		<selectcangku :message="{cangkuShow:cangkuShow}" @cangkuSure="cangkuSure"></selectcangku>
		<u-popup :show="bfchange" :round="10" mode="center">
			<view class="flexcenter margin_top">提示</view>
			<view class="zhengds">
				<view class="strongtext" style="text-align: center;">修改客户后可能导致某些数据流失</view>
				<view class="strongtext" style="text-align: center;">是否确认修改</view>
				<view class="flexcenter margin_top">
					<view class="btns btn1" @click="bfsure(1)">取消</view>
					<view class="btns btn2" @click="bfsure(2)">确定</view>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")
	export default {
		data() {
			return {
				// 下拉菜单
				selectshow: false,
				zdshow: false,
				xsshow: false,
				qkshow: false,
				yhshow: false,
				mrshow: false,
				// 仓库档案
				cangkuShow: false,
				cangku: {
					id: "",
					name: ""
				},
				qk_money:"",
				preferential_money: "", //优惠金额实际内容
				money: "", //优惠金额
				note: "", //整单备注实际内容
				note_text: "", //整单备注
				sj_current: 'wholesale_price' ,//售价方案'wholesale_price'批发价，market_sell_price零售价,last_price上次售价
				purchase_code:"",
				bf_data:{
					visit_id:"",//拜访id
					customers_id:"",//客户id
				},
				bf_model:false,//拜访模式，只有在拜访客户进来的时候才会开启
				bfchange:false,//是否改变客户弹出层
				getmsg:{
					id:"",
					type:""//1未推送，2待下单，3已下单
				},
				dataAll:{
					id: 1, //ID
					customers_id: 1, //客户ID
					stores_id: null, //仓库ID
					goods_kind: 2, //商品种类
					number: 11, //销货数量
					all_money: 363, //销货金额
					preferential_money: 0, //优惠金额
					real_money: 0, //优惠后金额
					status: "0", //状态:0=未打卡,1=正常,2=异常
					note: "", //备注
					customer_name: "kehu1", //客户
					arrear_account: -48, //总欠款
					receive_account: 30, //预收款
					settlementtype_name: "现金", //客户结算方式
					stores_name: null, //仓库
					goods_info:[]
				}
			}
		},
		onLoad(options) {
			uni.hideTabBar(); //隐藏tab
			let data = JSON.parse(decodeURIComponent(options.msg));
			this.getmsg = data
		},
		onShow() {
			this.selectshow = false
			this.init();
		},
		methods: {
			init() {
				let _this = this;
				httpRequest.request('appapi/manage/customer/push_detail', 'POST', {
					id:this.getmsg.id
				}, false, false, true).then(res => {
					this.dataAll = res.data
					this.cangku.id = res.data.stores_id
					this.cangku.name = res.data.stores_name
					this.note_text = res.data.note
				}).catch(err => {
					// httpRequest.toast(err.msg);
				})
			},
			switchsj(i) {
				this.sj_current = i
			},
			// 仓库档案确定
			cangkuSure(e) {
				this.cangkuShow = false
				if (e) {
					this.cangku.id = e.value[0].id
					this.cangku.name = e.value[0].name
				}
				this.refreshmsg()
			},
			// 数据刷新
			refreshmsg() {
				let _this = this
				httpRequest.request('appapi/manage/customer/push_add', 'POST', {
					id: this.getmsg.id,
					stores_id: this.cangku.id, //仓库id
					preferential_money: this.dataAll.preferential_money || 0, //优惠金额
					note:this.note_text
				}, false, false, true).then(res => {
					if(res.code==1){
						this.init()
					}else{
						httpRequest.toast(res.msg);
					}
				}).catch(err => {
					httpRequest.toast(err.msg);
				})
			},
			//跳转修改单据明细
			xgdjmx(item) {
				if(this.getmsg.type==3){
					return
				}
				if (this.selectshow == true) {
					this.selectshow = false
					return
				}
				let obj = {
					type:9,//1销售单，2退货单
					sales_info_id: item.id,
					store_id: this.cangku.id,
					price: this.sj_current
				}
				uni.navigateTo({
					url: '/pages/workbench/xiaoshou/xiugaidjmx?msg=' + encodeURIComponent(JSON.stringify(obj))
				})
			},
			//跳转历史单据
			lishidanju() {
				if (this.kehu.id == "") {
					httpRequest.toast('请选择客户')
					return false
				}
				let obj = {
					kh_id:this.kehu.id,
					type:0//0,销售单
				}
				uni.navigateTo({
					url: '/pages/workbench/xiaoshou/lishidanju?msg=' + encodeURIComponent(JSON.stringify(obj))
				})
			},
			// 提交单据
			submit() {
				let _this = this
				httpRequest.request('appapi/manage/goods/sales_submit', 'POST', {
					purchase_code: this.dataAll.purchase_code,
					visit_customer_id:this.bf_data.visit_id
				}, false, false, true).then(res => {
					httpRequest.toast(res.msg)
					if(res.code==1){
						setTimeout(function(){
							uni.navigateBack(1)
						},1000)
					}
				}).catch(err => {
					httpRequest.toast(err.msg);
				})
			},
			dian() {
				this.selectshow = this.selectshow == false ? true : false
			},
			hidefalse() {
				if (this.selectshow == true) {
					this.selectshow = false
					return
				}
			},
			zhengdanbz() {
				this.selectshow = false
				this.zdshow = true
			},
			// 跳转销售单-预售款金额
			yushoukuan() {
				let data = {
					customers_id: this.dataAll.customers_id
				}
				uni.navigateTo({
					url: '/pages/workbench/xiaoshou/xiaoshoudanxskje?msg=' + encodeURIComponent(JSON.stringify(
						data))
				})
			},
			//跳转添加商品页面
			tianjia(value) {
				if(this.getmsg.type==3){
					return
				}
				if (this.cangku.id == "") {
					httpRequest.toast("请选择仓库")
					return false
				}
				let data = {
					type:9,//1销货单，2退货单
					ck_id: this.cangku.id,
					sp_code: this.getmsg.id,
					mr_sjstate: this.sj_current,
					kh_id:"",
					yg_id:"",
					goods_ids: []
				}
				uni.navigateTo({
					url: '/pages/workbench/xiaoshou/tianjspxsdd?getmsg=' + encodeURIComponent(JSON.stringify(
						data))
				})
			},
			zdclose() {
				this.zdshow = false
				// console.log('close');
			},
			//整单备注
			xsclose() {
				this.xsshow = false
				this.note_text = ""
			},
			// 整单备注确认
			surenote() {
				this.xsshow = false
				this.note = this.note_text
				this.refreshmsg()
			},
			xiaosbz() {
				this.selectshow = false
				this.xsshow = true
			},
			//取消下但
			qingkong() {
				httpRequest.request('appapi/manage/customer/push_cancel', 'POST', {
					id: this.getmsg.id,
				}, false, false, true).then(res => {
					if(res.code==1){
						httpRequest.toast("取消成功");
						setTimeout(()=>{
							uni.navigateBack({
								delta:1
							})
						},1000)
					}else{
						httpRequest.toast(res.msg);
					}
				}).catch(err => {
					httpRequest.toast(err.msg);
				})
			},
			tuisong(){
				httpRequest.request('appapi/manage/customer/push_push', 'POST', {
					id: this.getmsg.id,
				}, false, false, true).then(res => {
					if(res.code==1){
						httpRequest.toast("推单成功");
						setTimeout(()=>{
							uni.navigateBack({
								delta:1
							})
						},1000)
					}else{
						httpRequest.toast(res.msg);
					}
				}).catch(err => {
					httpRequest.toast(err.msg);
				})
			},
			//优惠
			yhclose() {
				this.yhshow = false
				this.money = ""
			},
			// 优惠确认
			suremoney() {
				this.yhshow = false
				this.preferential_money = this.money
				this.refreshmsg()
			},
			youhui() {
				this.selectshow = false
				this.yhshow = true
			},
			//跳转默认售价
			morenshouj() {
				this.selectshow = false
				this.mrshow = true
			},
			mrclose() {
				this.mrshow = false
			},
		}
	}
</script>
<style lang="scss" scoped>
	.duihao {
		position: absolute;
		left: 0;
		background-image: url('@/static/image/system/duihao.png');
		width: 30rpx;
		height: 30rpx;
		background-size: 100% 100%;
	}

	.lansetextwbk {
		font-size: 28rpx;
		font-family: PingFang SC-Bold, PingFang SC;
		font-weight: bold;
		color: #0BBDCB;
		padding-right: 30rpx;
	}

	.juzhong {
		text-align: center;
	}

	.xiabiakuang {
		position: relative;
		padding-bottom: 30rpx;
		border-bottom: 2rpx solid rgba(153, 153, 153, .1);
		min-width: 500rpx;
	}

	// /deep/.u-popup__content {
	// 	width: 85%;
	// }

	// .fangan {
	// 	overflow: auto;
	// 	height: 800rpx;
	// }

	.zhengds {
		padding: 40rpx;
	}

	.shurudh {
		border-radius: 24rpx;
		height: 60rpx;
		font-size: 30rpx;
		width: 460rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #999999;
		background: rgba(217, 217, 217, .2);
		text-align: center;
	}

	.btns {
		width: 198rpx;
		height: 62rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 28rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
	}

	.btn2 {
		color: #FFFFFF;
		background: #0BBDCB;
		border-radius: 74rpx;
		margin-left: 30rpx;
	}

	.btn1 {
		border: 1rpx solid #0BBDCB;
		color: #0BBDCB;
		background: #FFFFFF;
		border-radius: 74rpx;
	}
</style>
<style lang="scss" scoped>
	.yuyin {
		margin-left: 16rpx;
	}

	.dibu {
		width: 100%;
		padding: 30rpx 30rpx;
		position: fixed;
		z-index: 1000;
		bottom: 0;
		left: 0;
		box-sizing: border-box;
	}

	.shuruk {
		width: 90%;
		background: rgba(217, 217, 217, .2);
		border-radius: 74rpx;
	}

	.inputs {
		height: 62rpx;
		width: 70%;
		font-size: 28rpx;
		padding-left: 44rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #999999;
	}

	.tianjia {
		height: 62rpx;
		width: 136rpx;
		background: rgba(11, 189, 203, .1);
		font-size: 28rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #0BBDCB;
		border-radius: 98px 98px 98px 98px;
	}

	.paddingsx {
		padding: 30rpx 0;
	}

	.xiaoshou {
		margin-right: 30rpx;
	}

	.hongse {
		color: #FF0000;
	}

	.lvse {
		color: #00B855;
	}

	.smalltext {
		font-size: 24rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #000000;
	}

	.shangpinxix {
		padding: 30rpx 30rpx 0;
	}

	.shangpinbox {
		padding-bottom: 30rpx;
		border-bottom: 2rpx solid rgba(153, 153, 153, 0.05);
	}

	.huisetext {
		font-size: 24rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #999999;
	}

	.jine {
		font-size: 24rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #00B855;
		padding-right: 30rpx;
	}

	.flex1s {
		flex: 1;
		text-align: center !important;
	}

	.flex2 {
		flex: 2;
	}

	.flex2s {
		flex: 2;
		text-align: center;
	}

	.flex1 {
		flex: 1;
		text-align: right;
	}

	.lansetext {
		font-size: 24rpx;
		text-decoration: underline #0BBDCB;
		font-family: PingFang SC-Bold, PingFang SC;
		font-weight: bold;
		color: #0BBDCB;
		margin-right: 10rpx;
	}

	.allbtn {
		width: 218rpx;
		height: 52rpx;
		border-radius: 50rpx;
		font-size: 24rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
	}

	.lansebtn {
		color: #0BBDCB;
		background: rgba(11, 189, 203, .1);
	}

	.huisebtn {
		color: #999999;
		background: rgba(153, 153, 153, .1);
	}

	// 定位点点点
	.diandiandian {
		position: relative;
	}

	.selectcontent {
		position: absolute;
		top: 50rpx;
		right: -6rpx;
		min-width: 250rpx;
		mix-width: 250rpx;
		text-align: center;
		background-color: rgba(11, 189, 203, 0.8);
		border-radius: 10rpx;
		font-size: 28rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #fff;
		word-wrap: break-word;
	}

	.sanjiao {
		position: absolute;
		top: -12rpx;
		right: 8rpx;
		background-image: url("@/static/image/system/sanjiao2.png");
		width: 40rpx;
		height: 12rpx;
		background-size: 100% 100%;
	}

	.leftimage {
		width: 34rpx;
		height: 34rpx;

		image {
			width: 100%;
			height: 100%;
		}
	}

	.itemselect {
		padding: 20rpx 30rpx;
	}

	.littlesmall {
		font-size: 28rpx;
		font-weight: bold;
	}
</style>
